﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar.Entity;
using RuoVea.OmiApi.Dict.Service;
using RuoVea.OmiApi.Dict.Service.Dto;
using RuoVea.OmiDict.Attributes;
using RuoVea.OmiDict.Language;

namespace RuoVea.OmiDict.Controllers;
/// <summary>
/// 
/// </summary>
public class SysDictTypePageParam : SysDictTypeParam
{
    private int _currentPage;
    /// <summary>
    /// 
    /// </summary>
    public int CurrentPage
    {
        get { return _currentPage; }
        set
        {
            _currentPage = value;  
            base.PageNo = value;  
        }
    }
}
/// <summary>
/// 
/// </summary>
[AppRoute]
public class DictTypeController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SysDictTypeService _sysDictTypService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysDictTypService"></param>
    public DictTypeController(SysDictTypeService sysDictTypService)
    {
        _sysDictTypService = sysDictTypService;
    }
    /// <summary>
    /// 参数配置页面
    /// </summary>
    [HttpGet(nameof(Index))]
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysDictTypeDto>>> GetPagesAsync()
    {
        RestfulResult<PageResult<SysDictTypeDto>> restfulResult = new() { Code = CodeStatus.OK };
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.no_upload_data);
        SysDictTypePageParam data = JsonConvert.DeserializeObject<SysDictTypePageParam>(dataStr);
        data ??= new SysDictTypePageParam { PageNo = 1, PageSize = 10 };
        restfulResult.Data = await _sysDictTypService.GetPagesAsync(data);
        return restfulResult;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(SysDictTypeDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _sysDictTypService.AddDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(SysDictTypeDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _sysDictTypService.UpdateDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(EntityBaseId data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _sysDictTypService.DeleteDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Message = ex.Message;
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            return restfulResult;
        }
    }
}
